
$(document).ready(function(){
	cw.common.getUserSession(validatePermission,true);
	
	$('#query').bind("click", searchNode)
	
	var url = cw.common.path + '/json?action=QUERY_D_TREE_ACTION';
	$.post(url,{},function(data){
		if(data.success){
			if(data.root.T_DEPARTMENT != null){
				var zNodes = [];
				$.each(data.root.T_DEPARTMENT.rs,function(i,n){
					var obj = {};
					obj.id = this.DEPARTMENT_NO;
					obj.pId = this.DEPARTMENT_PNO;
					obj.name = this.DEPARTMENT_NAME;
					obj.t = "id="+ this.DEPARTMENT_NO;
					obj.open = true;
					zNodes.push(obj);
				})
				$.fn.zTree.init($("#tree"), setting, zNodes);
			}
			
		}
	},'json');
	
	
});

var nodeList = [];
function searchNode(e) {
	
	var zTree = $.fn.zTree.getZTreeObj("tree");
	var value = $.trim($('#QUERY_NAME').val());
	var keyType = "name";
	updateNodes({color:"#333", "font-weight":"normal"});
	nodeList = zTree.getNodesByParamFuzzy(keyType, value);
	
	updateNodes({color:"#A60000", "font-weight":"bold"});

}
		
function updateNodes(fontCss) {
	var zTree = $.fn.zTree.getZTreeObj("tree");
	for( var i=0, l=nodeList.length; i<l; i++) {
		zTree.setting.view.fontCss = fontCss;
		zTree.updateNode(nodeList[i]);
	}
}

var setting = {
	view: {
		addHoverDom: addHoverDom,
		removeHoverDom: removeHoverDom,
		selectedMulti: false
	},
	edit: {
		enable: true
	},
	data: {
		key: {
			title: "t"
		},
		simpleData: {
			enable: true
		}
	},
	callback: {
		beforeDrag: beforeDrag,
		beforeRemove: beforeRemove,
		beforeRename: beforeRename,
		onRemove: onRemove,
		onRename: onRename
	}
};

var log, className = "dark";
function beforeDrag(treeId, treeNodes) {
	return false;
}

function beforeRemove(treeId, treeNode) {
	className = (className === "dark" ? "":"dark");
	var zTree = $.fn.zTree.getZTreeObj("tree");
	zTree.selectNode(treeNode);
	return confirm("确认删除 节点 -- " + treeNode.name + " 吗？");
}

function onRemove(e, treeId, treeNode) {
	$.get(cw.common.path+"/json?action=DELETE_T_DEPARTMENT_ACTION&date="+new Date(),{
		DEPARTMENT_NO : treeNode.id
	},function(data){
		if(data.success){
			
		}
		else{
			alert("删除失败");
		}
	},'json');
}

function beforeRename(treeId, treeNode, newName) {
	className = (className === "dark" ? "":"dark");
	if (newName.length == 0) {
		alert("组织结构名称不能为空.");
		return false;
	}
	return true;
}

function onRename(e, treeId, treeNode) {
	$.get(cw.common.path+"/json?action=UPDATE_T_DEPARTMENT_ACTION&date="+new Date(),{
		DEPARTMENT_NO : treeNode.id,
		DEPARTMENT_NAME : treeNode.name
	},function(data){
		if(data.success){
			
		}
		else{
			alert("修改失败");
		}
	},'json');
}

function addHoverDom(treeId, treeNode) {
	var sObj = $("#" + treeNode.tId + "_span");
	if ($("#addBtn_"+treeNode.id).length>0) return;
	var addStr = "<button type='button' class='add' id='addBtn_" + treeNode.id + "' title='add' onfocus='this.blur();'></button>";
	sObj.append(addStr);
	var btn = $("#addBtn_"+treeNode.id);
	if (btn) btn.bind("click", function(){
		createAddDialog(treeNode);
	});
};

function removeHoverDom(treeId, treeNode) {
	$("#addBtn_"+treeNode.id).unbind().remove();
};

var pnode;
var boxy;
function createAddDialog(node){
	pnode = node;
	var html ="<div>组织结构名称：<input type='text' id='DEPARTMENT_NAME' width='100px;'/></div>";
	html +="<div style='text-align:center'><input type='button' value='提交' onclick='submitForm(this);'/>" +
			"<input type='button' value='取消' onclick='Boxy.get(this).hide();'/></div>";
	
	if(boxy == null){
		boxy = new Boxy(html, {title: "新增组织结构", modal:true});
	}
	else{
		boxy.show();
	}
}

function submitForm(obj){
	var v = $.trim($('#DEPARTMENT_NAME').val());
	if(v.length == 0){
		alert("组织结构名称不能为空");
		return;
	}
	$.get(cw.common.path+"/json?action=ADD_T_DEPARTMENT_ACTION&date="+new Date(),{
		DEPARTMENT_NAME : v,
		DEPARTMENT_PNO : pnode.id
	},function(data){
		if(data.success){
			Boxy.get(obj).hide();
			var sid = data.root.ID.rs[0];
			var zTree = $.fn.zTree.getZTreeObj("tree");
			zTree.addNodes(pnode, {id:sid, pId:pnode.id, name:v, t:"id="+sid});
			$('#DEPARTMENT_NAME').val('');
		}
		else{
			alert("添加失败");
		}
	},'json');
}

//验证权限
function validatePermission(userinfo){
	var permissionNames = "add,del,update";
	var actionNames = "ADD_T_SYS_ROLE_ACTION,DELETE_T_SYS_ROLE_ACTION,UPDATE_T_SYS_ROLE_ACTION";
	cw.common.hasPermission(permissionNames,actionNames,isShow);
}
//是否显示操作按钮
function isShow(data){
	var obj = data.root.PERMISSION.rs[0];
	if(obj.add){
		$('#add').html('<a href="add_department.html"><img src="../images/anniu16.gif"/></a>');
	}
	del = obj.del;
	if(del){
		$('#batchDel').html('<a href="javascript:batchDelDepartment()">批量删除</a>');
	}
	update = obj.update;
}

function delDepartment(id,name){
	if(confirm("是否确定删除："+name+"信息?")){
		var url = cw.common.path + '/json?action=DELETE_T_DEPARTMENT_ACTION&DEPARTMENT_NO='+id;
		$.post(url,{},function(data){
			if(data.success){
				alert('已成功删除系统部门：'+name);
				location.href = document.URL;
			} else {
				alert(data.errors.errmsg);
			}
		},'json');
	}
}

function batchDelDepartment(){
	if(getCheckedNum('check')==0){
		alert('请选择要删除的组织结构');
		return;
	}
	if(!confirm('确定要删除吗？')){
		return;
	}
	$.post(cw.common.path + '/json',
		{
			action : 'BATCH_DELETE_T_DEPARTMENT_ACTION',
			DEPARTMENT_NO : getCheckedVal('check').split(',')
		}, function(data){
			if (data.success) {
				alert('批量删除成功');
				location.href = document.URL;
			} else {
				alert(data.errors.errmsg);
			}
		}, 'json');
}
